package com.algorithm.ch1.cjm.race;

import java.util.*;

public class FindReplaceString {

    public static void main(String[] args) {

        String s = "ejvzndtzncrelhedwlwiqgdbdgctgubzczgtovufncicjlwsmfdcrqeaghuevyexqdhffikvecuazrelofjmyjjznnjdkimbklrh";
        int[] indexs = new int[]{25,35,60,77,69,79,15,19,58,92,27,64,4,11,51,7,72,67,30,0,74,98,17,85,48,32,38,62,43,2,9,55,87};
        String[] sources = new String[]{"gc","tov","vy","re","ikv","lo","dw","iqgdbd","ue","kimbk","tgu","qd","ndt","elhe","crq","zn","ec","ff","bz","ej","ua","rh","lw","jj","mfd","cz","ufn","ex","cjl","vz","cr","agh","znnj"};
        String[] targets = new String[]{"dxs","hn","vfc","wnr","tira","rko","oob","mlitiwj","zrj","onpp","ot","c","lm","hbsje","dgc","ruf","qi","h","vzn","ja","ow","te","km","imq","pia","ixp","xsod","m","eat","yf","lzu","dgy","dyrsc"};

//        String replaceString = findReplaceString(s, indexs, sources, targets);
        String replaceString1 = findReplaceString1(s, indexs, sources, targets);

//        System.out.println(replaceString);


        String right = "jayflmruflzuhbsjeoobkmmlitiwjdxsotvznixpghnxsodcieatwspiadgcedgyzrjvfcmchhtiraqiowzwnrrkofjmyimqdyrscdonpplte";
        String curre = "jayflmruflzuhbsjeoobkmmlitiwjdxsotvznixpghnxsodcieatwspiadgcedgyzrjvfcmchhtiraqiowzrerkofjmyimqdyrscdonpplrh";

        System.out.println(right.equals(replaceString1));
    }

    /**
     * 注意，如果从后往前替换，能够避免位置问题
     *
     * @param s
     * @param indexes
     * @param sources
     * @param targets
     * @return
     */
    public static String findReplaceString1(String s, int[] indexes, String[] sources, String[] targets) {
        List<int[]> ls = new ArrayList<>();
        for (int i = 0; i < indexes.length; i++) {
            ls.add(new int[]{indexes[i], i});
        }
        Collections.sort(ls, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[0] - o1[0];
            }
        });
        for (int[] pair : ls) {
            int index = pair[0];
            int i = pair[1];
            String source = sources[i];
            String target = targets[i];
            if (s.substring(index, index + source.length()).equals(source)) {
                s = s.substring(0, index) + target + s.substring(index + source.length());
            }
        }
        return s;
    }

    /**
     * Find And Replace in String
     *
     * //TODO 草拟吗 为啥不可以？
     * @param s
     * @param indexes
     * @param sources
     * @param targets
     * @return
     */
    public static String findReplaceString(String s, int[] indexes, String[] sources, String[] targets) {

        int i = 0;
        int length = s.length();

        while (i < indexes.length){

            System.out.println("下标：" + i + ",坐标位置为：" + indexes[i] + ",要" +
                    "替换：" + sources[i] + ", 目标：" + targets[i]);

            int index = indexes[i];
            if(s.indexOf(sources[i]) == index) {

                String sufStr = s.substring(index, length);
                String preStr = s.substring(0, index);

                String tarStr = sufStr.replaceFirst(sources[i], targets[i]);

                System.out.println("替换前的：" + s);

                s = preStr + tarStr;

                System.out.println("替换后的：" + s);

                int range = targets[i].length() - sources[i].length();

                length += range;

                for(int j = i + 1 ; j < indexes.length; j++){

                    if(indexes[j] >= indexes[i]){
                        indexes[j] += range;
                    }
                }

                System.out.println(Arrays.toString(indexes));
            }
            i++;
        }
        return s;
    }
}
